﻿Imports Castle.ActiveRecord
Imports NHibernate
Imports NHibernate.Criterion

Namespace Model

    ''' <summary>
    '''	Criado em 21/08/2010 16:13:00
    ''' </summary>
    ''' <remarks></remarks>
    <Serializable(), ActiveRecord("municipio", DynamicUpdate:=True)> _
    Public Class Municipio
        Inherits ActiveRecordBase(Of Model.Municipio)


        Private _cd_municipio As Integer
        Private _municipio_nome As String
        Private _municipio_ibge As Decimal

        Private _Enderecos As IList(Of Model.Endereco)
        Private _Pessoas As IList(Of Model.Pessoa)
        Private _NFs As IList(Of Model.Nf)

        Private _Estado As Model.Estado

        <PrimaryKey(PrimaryKeyType.Sequence, SequenceName:="municipio_cd_municipio_seq")> _
        Public Property cd_municipio() As Integer
            Get
                Return _cd_municipio
            End Get
            Set(ByVal value As Integer)
                _cd_municipio = value
            End Set
        End Property

        <BelongsTo("cd_estado")> _
        Public Property Estado() As Model.Estado
            Get
                Return _Estado
            End Get
            Set(ByVal value As Model.Estado)
                _Estado = value
            End Set
        End Property

        <HasMany(GetType(Pessoa), Table:="pessoa", Lazy:=True, ColumnKey:="cd_municipio")> _
        Public Property Pessoas() As IList(Of Pessoa)
            Get
                Return _Pessoas
            End Get
            Set(ByVal value As IList(Of Pessoa))
                _Pessoas = value
            End Set
        End Property

        <HasMany(GetType(Endereco), Table:="endereco", Lazy:=True, ColumnKey:="cd_municipio")> _
        Public Property Enderecos() As IList(Of Endereco)
            Get
                Return _Enderecos
            End Get
            Set(ByVal value As IList(Of Endereco))
                _Enderecos = value
            End Set
        End Property

        <HasMany(GetType(Model.Nf), Table:="nf", Lazy:=True, ColumnKey:="cd_municipio")> _
        Public Property NFs() As IList(Of Model.Nf)
            Get
                Return _NFs
            End Get
            Set(ByVal value As IList(Of Model.Nf))
                _NFs = value
            End Set
        End Property


        <[Property]("municipio_nome")> _
        Public Property municipio_nome() As String
            Get
                Return _municipio_nome
            End Get
            Set(ByVal value As String)
                _municipio_nome = value
            End Set
        End Property

        <[Property]("municipio_ibge")> _
        Public Property municipio_ibge() As Decimal
            Get
                Return _municipio_ibge
            End Get
            Set(ByVal value As Decimal)
                _municipio_ibge = value
            End Set
        End Property

        Public Function GetByIBGE(ByVal Ibge As Integer) As Model.Municipio
            Dim q As DetachedCriteria = DetachedCriteria.For(Of Model.Municipio)("municipio")

            q.Add(Restrictions.Eq("municipio_ibge", CType(Ibge, Decimal)))

            Return Model.Municipio.FindFirst(q)
        End Function

        Public Function GetByEstado(ByVal Estado As Model.Estado) As IList(Of Model.Municipio)
            Dim q As DetachedCriteria = DetachedCriteria.For(Of Model.Municipio) _
                .CreateAlias("Estado", "estado")

            q.Add(Restrictions.Eq("Estado", Estado))

            Return Model.Municipio.FindAll(q)
        End Function

    End Class
End Namespace